﻿Imports System.IO
Imports System.Xml

Friend Class MessageHandler

    Friend Function ReadSMS(Text As String) As SMSMessage

        Dim strParts As String() = Split(Text, Chr(10), , CompareMethod.Text)

        Dim mySMS As New SMSMessage
        mySMS.ToPhone = ExtractValue(strParts(3), """", 3)
        mySMS.ToName = ExtractValue(strParts(4), """", 3)
        mySMS.Body = ExtractValue(strParts(6), """", 3)
        mySMS.FromPhone = ExtractValue(strParts(8), """", 3)
        mySMS.FromName = ExtractValue(strParts(9), """", 3)
        mySMS.Type = CLng(ExtractValue(strParts(11), ":", 1))
        mySMS.Status = CLng(ExtractValue(strParts(12), ":", 1))
        mySMS.DateTime = Utility.EpochToDate(CLng(ExtractValue(strParts(13), " ", 1)))

        Return mySMS

    End Function

    Friend Function ReadReceipt(Text As String) As SMSReceipt

        Dim strParts As String() = Split(Text, Chr(10), , CompareMethod.Text)

        Dim myRec As New SMSReceipt
        myRec.ToPhone = ExtractValue(strParts(4), """", 3)
        myRec.FromPhone = ExtractValue(strParts(5), """", 3)
        myRec.DateTime = Utility.EpochToDate(CLng(ExtractValue(strParts(7), ":", 1)))

        Return myRec

    End Function


    Friend Function CreateMessage(Item As SMSMessage) As String

        Dim SendDate As String = Utility.DateToEpoch(DateTime.Now)
        Item.DateTime = Utility.EpochToDate(CLng(SendDate))

        Dim output As String
        output = "{" & Chr(10) & """message"": {" & Chr(10) & """to"": {" & Chr(10) & """phone"": """
        output = output & Item.ToPhone & """," & Chr(10) & """name"": """ & Item.ToName & """" & Chr(10) & "},"
        output = output & Chr(10) & """body"": """ & Item.Body & """," & Chr(10) & """from"": {" & Chr(10)
        output = output & """phone"":""5555555555""," & Chr(10) & """name"": ""NULL_VALUE""" & Chr(10) & "},"
        output = output & Chr(10) & """type"": " & Item.Type & "," & Chr(10) & """status"": " & Item.Status & "," & Chr(10) & """dateTime"": " & SendDate
        output = output & Chr(10) & "}}"

        Return output

    End Function

    Friend Function ImportMessages(Text As String) As SMSMessages

        Dim xmldoc As New XmlDocument()
        Dim xmlnl As XmlNodeList
        Dim xmlnode As XmlNode
        Dim colS As New SMSMessages

        Dim fs As New FileStream(Text, FileMode.Open, FileAccess.Read)

        xmldoc.Load(fs)
        xmlnl = xmldoc.GetElementsByTagName("sms")

        For Each xmlnode In xmlnl
            Dim s As New SMSMessage

            s.Type = xmlnode.Attributes("type").Value
            s.DateTime = Utility.EpochToDate(xmlnode.Attributes("date").Value)
            s.Body = xmlnode.Attributes("body").Value

            Dim no As String = Replace(xmlnode.Attributes("address").Value, " ", "", , , CompareMethod.Text)

            'Need to look at doing some swapping around if the no contains a name 

            If s.Type = 1 Then  'Incoming
                s.ToName = "NULL_VALUE"
                s.ToPhone = "5555555555"
                s.FromName = xmlnode.Attributes("contact_name").Value
                s.FromPhone = no
                s.Read = True
                s.Type = 0
            Else    'Outgoing
                s.ToName = xmlnode.Attributes("contact_name").Value
                s.ToPhone = no
                s.FromName = "NULL_VALUE"
                s.FromPhone = "5555555555"
                s.Receipt = True
                s.Type = 1
            End If

            s.Status = xmlnode.Attributes("status").Value

            colS.Add(s)
        Next

        Return colS

    End Function


    Private Function ExtractValue(Text As String, SplitOn As String, SplitAt As Integer) As String

        Dim strSplit As String() = Split(Text, SplitOn, , CompareMethod.Text)
        Return strSplit(SplitAt)

    End Function




End Class
